package owg.util.editor2d;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import owg.util.awt.EnumEditor;
import owg.util.awt.StringFormattedEditor;
import owg.util.data.property.PropertyImmutable;
import owg.util.data.property.PropertyListener;
import owg.util.editor2d.Editor2D.Pivot;

public class MiscConfigPane extends JPanel
{
	public final Editor2DUI editor2dui;
	private static final long serialVersionUID = -6040664793278959977L;	
	
	private EnumEditor<Pivot> pivotEditor;
	private PropertyListener<Pivot> pivotListener;
	
	private StringFormattedEditor<Integer> snapScaleSizeEditor;
	private PropertyListener<Integer> snapScaleSizeListener;
	private JToggleButton snapScaleEnabledEditor;
	private PropertyListener<Boolean> snapScaleEnabledListener;

	private StringFormattedEditor<Integer> snapWidthEditor;
	private PropertyListener<Integer> snapWidthListener;
	private StringFormattedEditor<Integer> snapHeightEditor;
	private PropertyListener<Integer> snapHeightListener;
	private JToggleButton snapEnabledEditor;
	private PropertyListener<Boolean> snapEnabledListener;
	private JToggleButton nwSnapEditor;
	private PropertyListener<Boolean> nwSnapListener;
	private JToggleButton snapIntegerEditor;
	private PropertyListener<Boolean> snapIntegerListener;
	private JToggleButton snapSelectEditor;
	private PropertyListener<Boolean> snapSelectListener;
	private JToggleButton snapVisibleEditor;
	private PropertyListener<Boolean> snapVisibleListener;
	
	public MiscConfigPane(Editor2DUI editor2dui)
	{
		this.editor2dui = editor2dui;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(makeScaleConfigPanel());
		add(makeSnapConfigPanel());
	}
	
	private Component makeScaleConfigPanel()
	{
		String tip;
		JPanel uiConfigPanel = new JPanel();
		uiConfigPanel.setLayout(new BoxLayout(uiConfigPanel, BoxLayout.X_AXIS));
		
		tip =   "<html><body>" +
			"The pivot mode defines which of the selected objects has focus,<br/>" +
			"and the central point for scaling and rotation.<br/>" +
			"-For scaling, an object's spatial offset from the<br/>" +
			" pivot will be scaled in addition to its size.<br/>" +
			"-For rotation, an object's spatial offset from the<br/>" +
			" pivot will be rotated as well as its axes.<br/>" +
			"Tapping the mode hotkeys multiple times will yield different effects.<br/>" +
			"</body></html>";
		JLabel l;
		l = new JLabel("Pivot mode[,]: ");
		l.setToolTipText(tip);
		uiConfigPanel.add(l);
		
		pivotEditor = new EnumEditor<Editor2D.Pivot>(editor2dui.editor.settings.pivot.get());
		pivotListener = new PropertyListener<Editor2D.Pivot>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Editor2D.Pivot> property,
				Editor2D.Pivot oldValue, Editor2D.Pivot newValue) {
				pivotEditor.setValue(property.get());
			}
		};
		editor2dui.editor.settings.pivot.addListener(null, pivotListener);
		pivotEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.pivot.set(pivotEditor.getValue(), pivotListener);
			}});
		pivotEditor.setToolTipText(tip);
		uiConfigPanel.add(pivotEditor);
		
		//SNAP-SCALE
		
		tip = "If enabled, object scales will be snapped to a multiple of this value.";
		l = new JLabel(" Snap Scale: ");
		l.setToolTipText(tip);
		uiConfigPanel.add(l);
		snapScaleSizeEditor = new StringFormattedEditor<Integer>(editor2dui.editor.settings.snapScaleSize.get());
		snapScaleSizeListener = new PropertyListener<Integer>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Integer> property,
				Integer oldValue, Integer newValue) {
				snapScaleSizeEditor.setValue(property.get());
			}
		};
		editor2dui.editor.settings.snapScaleSize.addListener(null, snapScaleSizeListener);
		snapScaleSizeEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapScaleSize.set(snapScaleSizeEditor.getValue(), snapScaleSizeListener);
			}});
		snapScaleSizeEditor.setToolTipText(tip);
		uiConfigPanel.add(snapScaleSizeEditor);
		
		snapScaleEnabledEditor = new JToggleButton("Enabled[shift-S]",editor2dui.editor.settings.snapScaleEnabled.get());
		snapScaleEnabledListener = new PropertyListener<Boolean>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Boolean> property,
				Boolean oldValue, Boolean newValue) {
				snapScaleEnabledEditor.setSelected(property.get());
			}
		};
		editor2dui.editor.settings.snapScaleEnabled.addListener(null, snapScaleEnabledListener);
		snapScaleEnabledEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapScaleEnabled.set(snapScaleEnabledEditor.isSelected(), snapScaleEnabledListener);
			}});
		snapScaleEnabledEditor.setToolTipText(tip);
		uiConfigPanel.add(snapScaleEnabledEditor);
		return uiConfigPanel;
	}
	

	private Component makeSnapConfigPanel()
	{
		String tip;
		JPanel uiConfigPanel = new JPanel();
		uiConfigPanel.setLayout(new BoxLayout(uiConfigPanel, BoxLayout.X_AXIS));
		
		JLabel l;
		
		tip = "If enabled, object locations will be snapped to a grid with this block size.";
		l = new JLabel("Grid Size: ");
		l.setToolTipText(tip);
		uiConfigPanel.add(l);
		snapWidthEditor = new StringFormattedEditor<Integer>(editor2dui.editor.settings.snapWidth.get());
		snapWidthListener = new PropertyListener<Integer>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Integer> property,
				Integer oldValue, Integer newValue) {
				snapWidthEditor.setValue(property.get());
			}
		};
		editor2dui.editor.settings.snapWidth.addListener(null, snapWidthListener);
		snapWidthEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapWidth.set(snapWidthEditor.getValue(), snapWidthListener);
			}});
		snapWidthEditor.setToolTipText(tip);
		uiConfigPanel.add(snapWidthEditor);
		
		snapHeightEditor = new StringFormattedEditor<Integer>(editor2dui.editor.settings.snapHeight.get());
		snapHeightListener = new PropertyListener<Integer>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Integer> property,
				Integer oldValue, Integer newValue) {
				snapHeightEditor.setValue(property.get());
			}
		};
		editor2dui.editor.settings.snapHeight.addListener(null, snapHeightListener);
		snapHeightEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapHeight.set(snapHeightEditor.getValue(), snapHeightListener);
			}});
		snapHeightEditor.setToolTipText(tip);
		uiConfigPanel.add(snapHeightEditor);
		
		//l = new JLabel("Enabled[ctrl-G]: ");
		//l.setToolTipText(tip);
		//uiConfigPanel.add(l);
		
		snapEnabledEditor = new JToggleButton("Enabled[shift-G]",editor2dui.editor.settings.snapEnabled.get());
		snapEnabledListener = new PropertyListener<Boolean>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Boolean> property,
				Boolean oldValue, Boolean newValue) {
				snapEnabledEditor.setSelected(property.get());
				snapVisibleEditor.setEnabled(property.get());
				snapSelectEditor.setEnabled(property.get());
			}
		};
		editor2dui.editor.settings.snapEnabled.addListener(null, snapEnabledListener);
		snapEnabledEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapEnabled.set(snapEnabledEditor.isSelected(), snapEnabledListener);
				snapVisibleEditor.setEnabled(snapEnabledEditor.isSelected());
				snapSelectEditor.setEnabled(snapEnabledEditor.isSelected());
			}});
		snapEnabledEditor.setToolTipText(tip);
		uiConfigPanel.add(snapEnabledEditor);
		
		tip = "If enabled, the grid will be displayed on the screen.";
		snapVisibleEditor = new JToggleButton("Show", editor2dui.editor.settings.snapVisible.get());
		snapVisibleListener = new PropertyListener<Boolean>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Boolean> property,
				Boolean oldValue, Boolean newValue) {
				snapVisibleEditor.setSelected(property.get());
			}
		};
		editor2dui.editor.settings.snapVisible.addListener(null, snapVisibleListener);
		snapVisibleEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapVisible.set(snapVisibleEditor.isSelected(), snapVisibleListener);
			}});
		snapVisibleEditor.setToolTipText(tip);
		uiConfigPanel.add(snapVisibleEditor);
		
		
		tip = "If enabled, the snapped position will be used when choosing which object to highlight in the editor.";
		snapSelectEditor = new JToggleButton("Highlight", editor2dui.editor.settings.snapSelect.get());
		snapSelectListener = new PropertyListener<Boolean>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Boolean> property,
				Boolean oldValue, Boolean newValue) {
				snapSelectEditor.setSelected(property.get());
			}
		};
		editor2dui.editor.settings.snapSelect.addListener(null, snapSelectListener);
		snapSelectEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapSelect.set(snapSelectEditor.isSelected(), snapSelectListener);
			}});
		snapSelectEditor.setToolTipText(tip);
		uiConfigPanel.add(snapSelectEditor);
		
		
		tip = "If enabled, locations will snap to the north-west of a cell. Otherwise, snap to the nearest vertex.";
		
		nwSnapEditor = new JToggleButton("NW", editor2dui.editor.settings.nwSnap.get());
		nwSnapListener = new PropertyListener<Boolean>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Boolean> property,
				Boolean oldValue, Boolean newValue) {
				nwSnapEditor.setSelected(property.get());
			}
		};
		editor2dui.editor.settings.nwSnap.addListener(null, nwSnapListener);
		nwSnapEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.nwSnap.set(nwSnapEditor.isSelected(), nwSnapListener);
			}});
		nwSnapEditor.setToolTipText(tip);
		uiConfigPanel.add(nwSnapEditor);
		
		tip = "If enabled, locations will always be rounded down to integers.";
		snapIntegerEditor = new JToggleButton("Int", editor2dui.editor.settings.snapInteger.get());
		snapIntegerListener = new PropertyListener<Boolean>() {
			@Override
			public void valueChanged(PropertyImmutable<? extends Boolean> property,
				Boolean oldValue, Boolean newValue) {
				snapIntegerEditor.setSelected(property.get());
			}
		};
		editor2dui.editor.settings.snapInteger.addListener(null, snapIntegerListener);
		snapIntegerEditor.addActionListener(new ActionListener(){
			@Override public void actionPerformed(ActionEvent e)
			{
				editor2dui.editor.settings.snapInteger.set(snapIntegerEditor.isSelected(), snapIntegerListener);
			}});
		snapIntegerEditor.setToolTipText(tip);
		uiConfigPanel.add(snapIntegerEditor);
		
		
		return uiConfigPanel;
	}
}
